﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Entities;
using WisDom.SmartTMS.Enums;

namespace WisDom.SmartTMS.Entities
{
    /// <summary>
    /// 审核主表
    /// </summary>
   public class BusinessAudit:Entity<Guid>
    {
        /// <summary>
        /// 业务编号ID
        /// </summary>
        public Guid BusinessCode { get; set; }

        /// <summary>
        /// 业务类别编号ID
        /// </summary>
        public BusinessType BusinessTypeCode { get; set; }

        /// <summary>
        /// 审核结果0审核中，1同意，2拒绝
        /// </summary>
        public AuditState State { get; set; }

        /// <summary>
        /// 最后审核结束时间
        /// </summary>
        public DateTime? AuditTime { get; set; }

        /// <summary>
        /// 创建人ID
        /// </summary>
        public Guid? CreatorID { get; set; }

        /// <summary>
        /// 创建人姓名
        /// </summary>
        public String CreatorRealName { get; set; }

        /// <summary>
        /// 是否已删除
        /// </summary>
        public Boolean Deleted { get; set; }

        /// <summary>
        /// 创建时间(审核发启时间)
        /// </summary>
        public DateTime CreateTime { get; set; }
        /// <summary>
        /// 审核日志
        /// </summary>
        public List<BusinessAuditLogs> BusinessAuditLogs { get; private set; }

        /// <summary>
        /// 构造方法
        /// </summary>
        protected BusinessAudit()
        {

        }
        public BusinessAudit(Guid id,Guid businessCode, BusinessType businessType,DateTime createTime,Guid? creatorID, string approver) 
        {
            BusinessAuditLogs = new List<BusinessAuditLogs>(); //实例化审核日志集合
            Id = id;//主键
            BusinessCode = businessCode;//业务ID
            BusinessTypeCode = businessType;//业务类别
            CreateTime = createTime;//创建时间
            CreatorID = creatorID;//创建人ID
            State = AuditState.ToAudit;//审核状态
            AuditTime = null;//审核最后时间
            CreatorRealName = CreatorRealName;//创建人姓名
            Deleted = Deleted;//是否删除
        }
        /// <summary>
        /// 添加审批日志
        /// </summary>
        /// <param name="id">主键</param>
        /// <param name="processPersionCode">审核人</param>
        /// <param name="processPersionName">审核人姓名</param>
        public void AddAuditLogs(Guid id, Guid processPersionCode, string processPersionName,AuditState processResult)
        {
            //实例化审计日志
            BusinessAuditLogs auditLogs = new BusinessAuditLogs(id,this.Id,processPersionCode,processPersionName,processResult);

            //添加到审计日志集合中
            BusinessAuditLogs.Add(auditLogs);
        }

        /// <summary>
        /// 查找当前审批人审批的日志记录
        /// </summary>
        /// <param name="processPersionCode"></param>
        /// <returns></returns>
        public BusinessAuditLogs GetProcessPersionCode(Guid processPersionCode)
        {
            BusinessAuditLogs auditLogs = BusinessAuditLogs.Find(m => m.ProcessPersionCode == processPersionCode);
            return auditLogs;
        }
        /// <summary>
        /// 通过并终止审批或拒绝
        /// </summary>
        /// <param name="processPersionCode">当前审批人</param>
        /// <param name="state">审批结果(通过或终止)</param>
        /// <param name="remark">审批备注</param>
        public void EndAudit(Guid processPersionCode, string processPersonName, AuditState state, string remark)
        {
            //查找需要审批的日志
            BusinessAuditLogs auditLog = GetProcessPersionCode(processPersionCode);

            //修改审批状态
            auditLog.UptLogs(state, remark);

            this.State = state;                       //修改主表最终状态
            this.AuditTime = DateTime.Now;            //修改主表最终审批时间
            this.CreatorRealName = processPersonName; //修改主表最终人
        }
        /// <summary>
        /// 审批通过但不终止审批
        /// </summary>
        /// <param name="processPersionCode">当前审批人</param>
        /// <param name="processPersonName">当前审批人姓名</param>
        /// <param name="remark">当前审批人备注</param>
        public void ProcessAudit(Guid processPersionCode, string processPersonName, string remark,AuditState processResult)
        {
            //查找属于当前审批人的审批日志
            BusinessAuditLogs auditLog = GetProcessPersionCode(processPersionCode);

            //当前审批人通过审批
            auditLog.UptLogs(processResult, remark);

            this.State = processResult;
            this.CreatorRealName = processPersonName; //修改审批主表的最新审批人
        }

    }
}
